﻿using Shop.Collect;
using WeDonekRpc.Client.Attr;
using WeDonekRpc.Client.Interface;
using WeDonekRpc.SqlSugar;

namespace Shop.LocalEvent.Goods
{
    [LocalEventName("Public")]
    internal class InitGoodsStockEvent : IEventHandler<GoodsPublicEvent>
    {
        private readonly IGoodsStockCollect _Stock;
        private readonly IGoodsCollect _Goods;
        private readonly ITransactionService _Tran;
        public InitGoodsStockEvent (IGoodsStockCollect stock, IGoodsCollect goods, ITransactionService tran)
        {
            this._Tran = tran;
            this._Goods = goods;
            this._Stock = stock;
        }

        public void HandleEvent (GoodsPublicEvent data, string eventName)
        {
            using (ILocalTransaction tran = this._Tran.ApplyTran())
            {
                this._Stock.InitStock(data.Goods.Id, data.OpUserId, data.Stock);
                this._Goods.PublicComplete(data.Goods.Id);
                tran.Commit();
            }
        }
    }
}
